package sky.wicket;

import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sky.domain.Airport;
import sky.service.AirportService;
import sky.tro.finders.AirportFinder;
import sky.wicket.panels.AirportPanel;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 19.09.13
 * Time: 19:49
 * To change this template use File | Settings | File Templates.
 */
public class AirportList extends BasePage {

    @SpringBean
    AirportService airportService;
    List<Airport> airportList;

    public AirportList() {
        airportList = airportService.list();
        addElementsToPage();
    }

    public AirportList(AirportFinder airportFinder) {
        airportList = airportService.listByFinder(airportFinder);
        addElementsToPage();
    }

    private void addElementsToPage() {
        //add filter link
        MyWicketUtils.addBookmarkablePageLinkWithLabel(this, "airportFilterLink", AirportFilter.class,
                        "airportFilterLabel", "Filter");
        //add airport link
        MyWicketUtils.addBookmarkablePageLinkWithLabel(this, "addAirportLink", AddUpdateAirport.class,
                        "addAirportLabel", "Add Airport");
        //list of flights
        ListView<Airport> airportListView = new ListView<Airport>("airportList", airportList) {
            @Override
            protected void populateItem(ListItem<Airport> component) {
                final Airport airport = component.getModelObject();
                addComponentsToList(component, airport);
            }
        };
        add(airportListView);
    }

    private void addComponentsToList(ListItem<Airport> component, final Airport airport) {
        component.add(new AirportPanel("airportPanel", new Model<Airport>(airport), true));
    }
}
